/**
 * 页面配置处理工具
 * 本文件用于处理项目的页面路由配置
 */

const PagesOrderSuccess = "pages/order/success";
const PagesOrderList = "pages/order/list";
const PagesOrderDetail = "pages/order/detail";
const PagesOrderPay = "pages/order/pay";
const PagesOrderCreate = "pages/order/create";

/**
 * 获取页面列表
 */
export function getPagesConfig() {
  return {
    // 登录相关页面
    loginPages: [
      "pages/index/log/login",
      "pages/index/log/register",
      "pages/index/log/forgot"
    ],
    // 首页和分类等主要页面
    mainPages: [
      "pages/index/index",
      "pages/category/category",
      "pages/cart/cart",
      "pages/user/user"
    ],
    // 商品相关页面
    productPages: [
      "pages/product/detail",
      "pages/product/list",
      "pages/search/search"
    ],
    // 订单相关页面
    orderPages: [
      "pages/order/success",
      "pages/order/list",
	    "pages/order/detail",
      "pages/order/pay",
      "pages/order/create"
    ]
  };
}

/**
 * 处理页面路由
 * @param {string} pagePath 页面路径
 * @param {object} params 参数
 */
export function navigateTo(pagePath, params = {}) {
  let url = '/' + pagePath;
  
  // 构建查询参数
  if (Object.keys(params).length > 0) {
    const queryString = Object.keys(params)
      .map(key => `${key}=${encodeURIComponent(params[key])}`)
      .join('&');
    url += `?${queryString}`;
  }
  
  // 使用uni api进行跳转
  uni.navigateTo({
    url
  });
}

/**
 * 处理TabBar页面跳转
 * @param {string} pagePath Tab页面路径
 */
export function switchTab(pagePath) {
  uni.switchTab({
    url: '/' + pagePath
  });
}

export default {
  getPagesConfig,
  navigateTo,
  switchTab,
  PagesOrderSuccess,
  PagesOrderList,
  PagesOrderDetail,
  PagesOrderPay,
  PagesOrderCreate
}; 